<?php

	// TITLE: Donation AUTO MODEL
	// FILE: scaffolding/model/donation.php
	// AUTHOR: AUTOGEN
	// CREATED: 2015/04/20 07:49:57
	// DESCRIPTION: 
	// REQUIREMENTS: 

	$createDonation = function($con, $donation) {
		$sql = 'INSERT INTO donation (id, donor_id, date, details, type_id, purpose_id, pledge_id, who_entered, when_entered, who_authorized, when_authorized) VALUES (';
		$sql .= $donation->getId() . ', ';
		$sql .= $donation->getDonorId() . ', ';
		$sql .= $donation->getDate() . ', ';
		$sql .= $donation->getDetails() . ', ';
		$sql .= $donation->getTypeId() . ', ';
		$sql .= $donation->getPurposeId() . ', ';
		$sql .= $donation->getPledgeId() . ', ';
		$sql .= $donation->getWhoEntered() . ', ';
		$sql .= $donation->getWhenEntered() . ', ';
		$sql .= $donation->getWhoAuthorized() . ', ';
		$sql .= $donation->getWhenAuthorized() . ')';
		$sql .= ')';
		$result = mysqli_query($con, $sql);
		$id = ($result) ? mysqli_insert_id($con) : $result;
		mysqli_close($con);
		return $id;
	};

	$readDonation = function($con, $id) {
		$sql = 'SELECT * FROM donation WHERE id = ' . $id;
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$result = mysqli_fetch_array($results);
			$donation = new Donation();
			$donation->setId(result[0]);
			$donation->setDonorId($dbio->select($readDonor(result[1])));
			$donation->setDate(result[2]);
			$donation->setDetails(result[3]);
			$donation->setTypeId($dbio->select($readType(result[4])));
			$donation->setPurposeId($dbio->select($readPurpose(result[5])));
			$donation->setPledgeId($dbio->select($readPledge(result[6])));
			$donation->setWhoEntered(result[7]);
			$donation->setWhenEntered(result[8]);
			$donation->setWhoAuthorized(result[9]);
			$donation->setWhenAuthorized(result[10]);
		} else {
			$donation = false;
		}
		return $donation;
	};

	$updateDonation = function($con, $donation) {
		$sql = 'UPDATE donation SET ';
		$sql .= 'id = ' . $donation->getId() . ', ';
		$sql .= 'donor_id = ' . $donation->getDonorId() . ', ';
		$sql .= 'date = ' . $donation->getDate() . ', ';
		$sql .= 'details = ' . $donation->getDetails() . ', ';
		$sql .= 'type_id = ' . $donation->getTypeId() . ', ';
		$sql .= 'purpose_id = ' . $donation->getPurposeId() . ', ';
		$sql .= 'pledge_id = ' . $donation->getPledgeId() . ', ';
		$sql .= 'who_entered = ' . $donation->getWhoEntered() . ', ';
		$sql .= 'when_entered = ' . $donation->getWhenEntered() . ', ';
		$sql .= 'who_authorized = ' . $donation->getWhoAuthorized() . ', ';
		$sql .= 'when_authorized = ' . $donation->getWhenAuthorized() ;
		$sql .= 'WHERE id = ' . id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$deleteDonation = function($con, $id) {
		$sql = 'DELETE FROM donation WHERE id = ' . $id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$listDonation = function($con) {
		$sql = 'SELECT * FROM donation';
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$donations = array();
			while ($result = mysqli_fetch_array($results)) {
				$donation = new Donation();
				$donation->setId(result[0]);
				$donation->setDonorId($dbio->select($readDonor(result[1])));
				$donation->setDate(result[2]);
				$donation->setDetails(result[3]);
				$donation->setTypeId($dbio->select($readType(result[4])));
				$donation->setPurposeId($dbio->select($readPurpose(result[5])));
				$donation->setPledgeId($dbio->select($readPledge(result[6])));
				$donation->setWhoEntered(result[7]);
				$donation->setWhenEntered(result[8]);
				$donation->setWhoAuthorized(result[9]);
				$donation->setWhenAuthorized(result[10]);
				$donations[] = donation;
			}// end while
		} else {
			$donations = false;
		}
		return $donations;
	};

